$(document).ready(function(){
    findList("",1,0);
    $(".UMsearch").click(function(){
        var name=$(".UMrole .name").val();
        var roleid=$(".UMrole .roleid").val();;
        var startPage=0;
        searchObj.name=name;
        searchObj.roleid=roleid;
        searchObj.page=startPage;
        findList(name,roleid,startPage);
    });
    $(".fa-step-backward").click(function(){
        if(searchObj.page==0){
            alert("当前已经是第一页");
        }
        else{
            findList(searchObj.name,searchObj.roleid,0);
        }
    });
    $(".fa-chevron-left").click(function(){
        if(searchObj.page==0){
            alert("当前已经是第一页");
        }
        else{
            searchObj.page--;
            findList(searchObj.name,searchObj.roleid,searchObj.page);
        }
    });
    $(".fa-chevron-right").click(function(){
        if(searchObj.page==searchObj.pages-1){
            alert("当前已经是最后一页");
        }
        else{
            searchObj.page++;
            findList(searchObj.name,searchObj.roleid,searchObj.page);
        }
    });
    $(".fa-step-forward").click(function(){
        if(searchObj.page==searchObj.pages-1){
            alert("当前已经是最后一页");
        }
        else{
            findList(searchObj.name,searchObj.roleid,searchObj.pages-1);
        }
    });   
     $(".subedit").click(function(){
        var length = $(".UMtab table tbody tr.tdcolorselected").length;
        if(length==0){
            alert("请在表格中选择一行");
        }
        else{
            var userid = $(".UMtab table tbody tr.tdcolorselected").eq(0).attr("userid");
            localStorage.setItem("userid",userid);
            location.href="AddUser.html";
        }
    });
});
var searchObj={};
function findList(name,roleid,startPage){
    searchObj.name=name;
    searchObj.roleid=roleid;
    $(".UMtab table tbody tr").remove();
    $.ajax({
        type: "POST",
        url: "./UserList",
        data: "name="+name+"&roleid="+roleid+"&startPage="+startPage,
        success: function(info){
            var json = JSON.parse(info);
            if(json.msg=="success"){
                searchObj.page=json.page.page;
                searchObj.total=json.page.total;
                var html="";
                for(i = 0; i<json.infoList.length;i++){
                    var gender=json.infoList[i].gender=="M"?"Male":"Female";
                    var role=json.infoList[i].roleId==1?"User office":"Manager";
                    var tr="<tr class='tdcolor' onclick='trClick(this)' userid='"+json.infoList[i].userId+"'>"+
                    "<td>"+json.infoList[i].email+"</td>"+
                    "<td>"+json.infoList[i].firstName+ " "+json.infoList[i].lastName+"</td>"+
                    "<td>"+gender+"</td>"+
                    "<td>"+json.infoList[i].dateOfBirth+"</td>"+
                    "<td>"+json.infoList[i].phone+"</td>"+
                    "<td>"+role+"</td>"+
                    "</tr>";
                    html+=tr;
                }
                $(".UMtab table tbody").append(html);
                $(".UMtab table tbody tr:odd").removeClass("tdcolor").addClass("tdcolor1");
                initNum(searchObj.total,".NUM select");
                $(".underpage .pages").text("Total Pages:"+searchObj.pages);
                $(".underpage .total").text("Total Records:"+searchObj.total);
            }
        }
     });
}

function initNum(total,select){
    var pages=parseInt(total/10) +1;
    if(parseInt(total)%10 == 0){
        pages--;
    }
    searchObj.pages=pages;
    $(select +" option").remove();
    for(var i = 0; i<pages;i++){
        if(searchObj.page==i){
            $(select ).append("<option  value='"+i+"' selected>"+(i+1)+"</option>");
        }
        else{
            $(select ).append("<option  value='"+i+"'>"+(i+1)+"</option>");
        }
     }
}

function trClick(obj){
    $(".UMtab table tbody tr").removeClass("tdcolorselected");
    $(obj).addClass("tdcolorselected");
}